import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {InitData, List, Detail, Part, Dealer, RaisingRate} from './DTO';
const baseUrl = '/parts/api/v1';

const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '备件销售订单'
});

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});

const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '新增{resourceName}'
}, {resourceName});

const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑{resourceName}'
}, {resourceName});

const submitDesc = formatMessage({
    id: 'api.message.submitDesc',
    defaultMessage: '提交{resourceName}'
}, {resourceName});

const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: '作废{resourceName}'
}, {resourceName});

const exportDesc = formatMessage({
    id: 'api.exportDesc',
    defaultMessage: '导出{resourceName}'
}, {resourceName});

const confirmDesc = formatMessage({
    id: 'api.confirmDesc',
    defaultMessage: '确认{resourceName}'
}, {resourceName});

const returnDesc = formatMessage({
    id: 'api.returnDesc',
    defaultMessage: '退回{resourceName}'
}, {resourceName});

const getPartsDesc = formatMessage({
    id: 'api.message.getPartsDesc',
    defaultMessage: '查询备件'
});

const getDealerDesc = formatMessage({
    id: 'api.message.getDealerDesc',
    defaultMessage: '查询总代'
});

const getRaisingRateDesc = formatMessage({
    id: 'api.message.getRaisingRateDesc',
    defaultMessage: '获取加价率'
});
// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/partSaleOrder/init`, null, InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery));

// 查询备件
export const getPartSaleOrders = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/partSaleOrders/forCurrentUser?${queryString}`, null, List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询详情
export const getPartSaleOrdersDetail = id => request(`${baseUrl}/partSaleOrders/${id}`, null, Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getDetailDesc));

// 新增
export const addPartSaleOrders = data => request(`${baseUrl}/partSaleOrders`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));

// 编辑
export const updatePartSaleOrders = (id, data) => request(`${baseUrl}/partSaleOrders/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));

// 提交
export const submitPartSaleOrders = (id, data) => request(`${baseUrl}/partSaleOrders/${id}/submit`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, submitDesc));

// 作废
export const abandonPartSaleOrders = (id, data) => request(`${baseUrl}/partSaleOrders/${id}/abandon`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));


// 确认
export const confirmPartSaleOrders = (id, data) => request(`${baseUrl}/partSaleOrders/${id}/confirm`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, confirmDesc));

export const returnPartSaleOrders = (id, data) => request(`${baseUrl}/partSaleOrders/${id}/return`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, returnDesc));

// 导出
export const exportPartSaleOrders = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/partSaleOrders/export/forCurrentUser?${queryString}`, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc))
        .then(res => res.data);
};

// 查询备件信息
export const getParts = queryCondition => {
    const queryString = stringify({
        ...queryCondition,
        isAccessory: false,
    });
    return request(`${baseUrl}/parts/forPartSaleOrder?${queryString}`, null, Part)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getPartsDesc));
};

// 查询经销商
export const getDealers = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/dealers/forPartSaleOrder?${queryString}`, null, Dealer)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerDesc));
};

// 快速查询经销商
export const getDealersSimple = query => {
    const queryString = stringify({
        query
    });
    return request(`${baseUrl}/dealers/forPartSaleOrder/simple?${queryString}`, null, Dealer)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerDesc))
        .then(res => {
            if(res.ok) return res.data.content;
            return false;
        });
};

export const getRaisingRate = (dealerId, typeId) =>
    request(`${baseUrl}/dealers/${dealerId}/partSaleOrderTypes/${typeId}`, null, RaisingRate)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getRaisingRateDesc));
